package com.yanya.week04.cglib;

import com.yanya.week04.ChainAbleInvocationContext;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import javax.interceptor.InvocationContext;
import java.lang.reflect.Method;

public class MethodInterceptorAdapter implements MethodInterceptor {
    private final Object target;

    private final Object[] interceptors;

    public MethodInterceptorAdapter(Object target, Object[] interceptors) {
        this.target = target;
        this.interceptors = interceptors;
    }
    @Override
    public Object intercept(Object object, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        InvocationContext delegateContext = new CglibMethodInvocationContext(object, method, methodProxy, objects);
        ChainAbleInvocationContext context = new ChainAbleInvocationContext(delegateContext, interceptors);
        return context.proceed();
    }
}
